<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <canvas id="webgpu" width="500" height="500"></canvas>
    <script type="module">
        // WebGPU 坐标系：WebGPU坐标系在Canvas画布上的坐标原点是Canvas画布的中间位置，x轴水平向右，y轴竖直向上，z轴垂直与Canvas画布，朝向屏幕内
        
        const canvas = document.getElementById('webgpu');
        const context = canvas.getContext('webgpu');
        const format = navigator.gpu.getPreferredCanvasFormat();
        
        context.configure({
            device: device,
            format: format
        });
        
        const adapter = await navigator.gpu.requestAdapter();
        const device = await adapter.requestDevice();
        
        const vertexArray = new Float32Array([
            0.0, 0.0, 0.0,
            0.0, 1.0, 0.0,
            1.0, 0.0, 0.0
        ]);
        
        // 创建顶点缓冲区
        const vertexBuffer = device.createBuffer({
            size: vertexArray.byteLength,   // 数据字节长度
            usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST
        });
        
        // 顶点数据写入顶点缓冲区
        device.queue.writeBuffer(vertexBuffer, 0, vertexArray);
        
        // 创建渲染管线
        const pipeline = device.createRenderPipeline({
            layout: 'auto',
            // 顶点相关配置
            vertex: {
                // 顶点着色器
                module: device.createShaderModule({ code: vertex }),
                entryPoint: 'main'
            },
            fragment: {
                // 片元着色器
                module: device.createShaderModule({ code: fragment }),
                entryPoint: 'main'
            },
            primitive:{
                topology: "triangle-list"
            }
        });
    </script>
</body>
</html>